package nongchang.kuaijiecaifu.com.ncapp_android.common.activity;

import android.content.pm.ActivityInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;


import com.trello.rxlifecycle.components.support.RxAppCompatActivity;
import com.umeng.analytics.MobclickAgent;


import butterknife.ButterKnife;
import nongchang.kuaijiecaifu.com.ncapp_android.R;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.API;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.base.Response;
import nongchang.kuaijiecaifu.com.ncapp_android.ui.widget.lib.SwipeBackLayout;
import nongchang.kuaijiecaifu.com.ncapp_android.ui.widget.lib.Utils;
import nongchang.kuaijiecaifu.com.ncapp_android.ui.widget.lib.app.SwipeBackActivityBase;
import nongchang.kuaijiecaifu.com.ncapp_android.ui.widget.lib.app.SwipeBackActivityHelper;
import nongchang.kuaijiecaifu.com.ncapp_android.util.DialogUtil;
import rx.Observable;

public class BaseActivity extends RxAppCompatActivity implements SwipeBackActivityBase {

    private TextView mToolbarTitle;
    private Toolbar mToolbar;
    private ActionBar mActionBar;
    private SwipeBackActivityHelper mHelper;
    private SwipeBackLayout mSwipeBackLayout;
    public <T extends Response> Observable.Transformer<T, T> checkOn() {
        return API.checkOn(this);
    }

    public <T extends Response> Observable.Transformer<T, T> catchOn() {
        return API.catchOn(this);
    }
    public void homeAsUp() { // 0xFFFFFFFF, 26
        mActionBar.setDisplayHomeAsUpEnabled(true);
        mToolbar.setContentInsetsAbsolute(0, 0);

    }

    public void setupToolbar(@IdRes int resId) {
        mToolbar = (Toolbar)findViewById(resId);
        setSupportActionBar(mToolbar);
        mActionBar = getSupportActionBar();
        mActionBar.setHomeAsUpIndicator(R.mipmap.ic_back_white);
    }
    public void setupToolbarWithHome(@IdRes int resId) {
        setupToolbar(resId);
        homeAsUp();
    }
    public Toolbar getToolbar() {
        return mToolbar;
    }

    @Override
    public void setSupportActionBar(android.support.v7.widget.Toolbar toolbar) {
        super.setSupportActionBar(toolbar);

        if (toolbar != null) {
            mToolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
            if (mToolbarTitle != null) {
                getSupportActionBar().setDisplayShowTitleEnabled(false);
            }
        }
    }

    @Override
    protected void onTitleChanged(CharSequence title, int color) {
        super.onTitleChanged(title, color);
        if (mToolbarTitle != null) {
            mToolbarTitle.setText(title);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
        DialogUtil.show(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        mHelper = new SwipeBackActivityHelper(this);
        mHelper.onActivityCreate();
        mSwipeBackLayout = getSwipeBackLayout();

        mSwipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mHelper.onPostCreate();
    }
    @Override
    public SwipeBackLayout getSwipeBackLayout() {
        return mHelper.getSwipeBackLayout();
    }
    @Override
    public void setSwipeBackEnable(boolean enable) {
        getSwipeBackLayout().setEnableGesture(enable);
    }
    @Override
    public void scrollToFinishActivity() {
        Utils.convertActivityToTranslucent(this);
        getSwipeBackLayout().scrollToFinishActivity();
    }
}